package com.medical.controller;

import com.medical.entity.UserEntity;
import com.medical.entity.dto.UpdateUserDto;
import com.medical.entity.vo.PrescriptionVo;
import com.medical.service.UserService;
import com.medical.tool.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户登录
     *
     * @param userAccount
     * @param userPassword
     * @return
     */
    @PostMapping("/login")
    public R login(String userAccount, String userPassword) {
        try {
            UserEntity userEntity = userService.login(userAccount, userPassword);
            if (userEntity == null) return new R(400, "账号或密码有误！");
            //httpServletRequest.getSession().setAttribute("USER_SESSION", user);
            return new R(200, "登陆成功!", userEntity);
        } catch (Exception exception) {
            exception.printStackTrace();
            return new R(500, "系统出现错误！");
        }
    }


    /**
     * 用户注册
     *
     * @param
     * @return
     */
    @PostMapping("/register")
    public R register(String userAccount, String userPassword, String userName) {
        boolean success = userService.register(userAccount, userPassword, userName);
        if (success) {
            return new R(200, "注册成功");
        } else {
            return new R(400, "用户名已存在，请重新注册");
        }
    }

    /**
     * 用户信息查询
     *
     * @return
     */
    @GetMapping("/selectList")
    public R selectList() {
        try {
            List<UserEntity> userEntity = userService.selectList();
            if (userEntity == null) return new R(400, "该用户不存在");
            return new R(200, "查询成功!", userEntity);
        } catch (Exception exception) {
            exception.printStackTrace();
            return new R(500, "系统出现错误！");
        }
    }

    /**
     * 更新用户信息
     *
     * @param userEntity
     * @return
     */
    @PutMapping("/update")
    public R update(@RequestBody UserEntity userEntity) {
        int result = userService.update(userEntity);
        if (result > 0) {
            return new R(200, "更新成功", userEntity);
        } else {
            return new R(400, "更新失败");
        }
    }
    @PutMapping("/updateById")
    public R updateById(@RequestBody UpdateUserDto updateUserDto) {
        int result = userService.updateById(updateUserDto.getUserId(),updateUserDto.getPassword());
        if (result > 0) {
            return new R(200, "更新成功");
        } else {
            return new R(400, "更新失败");
        }
    }


    /**
     * 用户注销
     *
     * @param
     * @return
     */
    @DeleteMapping("/logout")
    public R logout(@RequestParam String userAccount) {
        boolean result = userService.delete(userAccount);
        if (result) {
            return new R(200, "注销成功");
        } else {
            return new R(400, "注销失败");
        }
        //return new R(200,"注销成功",userService.delete(userAccount));
    }

    /**
     * 查询用户详情
     * @param userId
     * @return
     */
    @GetMapping("/details")
    public R selectByIdDetails(@RequestParam String userId) {
        try {
            PrescriptionVo userEntity = userService.selectByIdDetails(userId);
            if (userEntity == null) return new R(400, "该用户不存在");
            return new R(200, "查询成功!", userEntity);
        } catch (Exception exception) {
            exception.printStackTrace();
            return new R(500, "系统出现错误！");
        }
    }

//    @PostMapping("/addUserDesc")
//    public R insertUserDesc(UserDescEntity userDescEntity){
//        int result = userService.insertUserDesc(userDescEntity);
//        if (result > 0){
//            return new R(200,"添加成功",userDescEntity);
//        } else {
//            return new R(400,"添加失败");
//        }
//    }


//    /**
//     * 用户注销
//     * 通过udid
//     * @param userEntity
//     * @param userDescEntity
//     * @return
//     */
////    @DeleteMapping("/logout")
////    public String logout (UserEntity userEntity, UserDescEntity userDescEntity){
////        boolean result = userService.deleteUser(userEntity) && userService.deleteUserDesc(userDescEntity);
////        return result ? "删除成功" : "删除失败";
////    }
//    @DeleteMapping("/logout")
//    public R logout (UserEntity userEntity, UserDescEntity userDescEntity){
//        boolean result = userService.deleteUser(userEntity) && userService.deleteUserDesc(userDescEntity);
//        if (result){
//            return new R(200,"注销成功");
//        } else {
//            return new R(400,"注销失败");
//        }
//    }

//
//    /**
//     * 用户详细信息
//     */
//    /**
//     * 添加用户详细信息
//     * @param userDescEntity
//     * @return
//     */
////    @PostMapping("/addUserDesc")
////    public String insertUserDesc(UserDescEntity userDescEntity){
////        int result = userService.insertUserDesc(userDescEntity);
////        return result > 0 ? "添加成功" : "添加失败";
////    }
//
//    @PostMapping("/addUserDesc")
//    public R insertUserDesc(UserDescEntity userDescEntity){
//        int result = userService.insertUserDesc(userDescEntity);
//        if (result > 0){
//            return new R(200,"添加成功",userDescEntity);
//        } else {
//            return new R(400,"添加失败");
//        }
//    }
//
//    /**
//     * 查询用户详细信息
//     * @param udId
//     * @return
//     */
//    @PostMapping("/findUserDesc")
//    public R findByUdId(String udId) {
//        //userService.findByUdId(udId);
//        return new R(200,"查询成功",userService.findByUdId(udId));
//    }
//
//    /**
//     * 更新用户详细信息
//     * @param userDescEntity
//     * @return
//     */
////    @PutMapping("/updateUserDesc")
////    public String updateUserDesc( UserDescEntity userDescEntity) {
////        boolean result = userService.updateUserDesc(userDescEntity);
////        return result  ? "更新成功" : "更新失败";
////    }
//    @PutMapping("/updateUserDesc")
//    public R updateUserDesc( UserDescEntity userDescEntity) {
//        boolean result = userService.updateUserDesc(userDescEntity);
//        if (result){
//            return new R(200,"更新成功");
//        } else {
//            return new R(400,"更新失败");
//        }
//    }

}
